package com.adaydayup.week3;

import java.util.Scanner;
import java.util.Calendar;

public class MyCalendar {
    public static void main(String[] args) {
        //保存用户输入的年份
        int year;
        //保存每个月的天数
        int days = 0;
        //保存月份
        String[] months;

        months = new String[13];
        months[1] = "January";
        months[2] = "February";
        months[3] = "March";
        months[4] = "April";
        months[5] = "May";
        months[6] = "June";
        months[7] = "July";
        months[8] = "August";
        months[9] = "September";
        months[10] = "October";
        months[11] = "November";
        months[12] = "December";

        //接受用户输入的年份
        Scanner in = new Scanner(System.in);
        System.out.print("Please input years:");
        year = in.nextInt();

        //得到一个Calendar对象
        Calendar c = Calendar.getInstance();

        //循环 12 个月
        for (int month = 1; month <= 12; month++) {
            //标题
            System.out.println("\n  Month's name is " + months[month]);
            for (int i = 1; i <= 27; i++) System.out.print("=");
            System.out.println("\nSun\tMon\tTue\tWed\tThu\tFri\tSat");

            // 判断当前月份的天数
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                days = 31;
            }
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                days = 30;
            }
            if (month == 2) {
                //闰年判断
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    days = 29;
                } else {
                    days = 28;
                }
            }

            //设置日期为当前月份的第一天,由于时区的原因，month-1,get(java.util.Calendar.DAY_OF_WEEK) - 1
            c.set(year, month - 1, 1);
            //求出第一天是星期几
            int FirstDayInWeek = c.get(Calendar.DAY_OF_WEEK) - 1;

            //用来控制换行
            int cnt = 0;

            //前面的空位
            for (int j = 0; j < FirstDayInWeek; j++) {
                System.out.print("    ");
                cnt++;
            }

            //打印当前月份日历
            for (int i = 1; i <= days; i++) {
                if (cnt == 7) {
                    System.out.printf("\n");
                    cnt = 0;
                }
                System.out.printf("%-4d", i);
                cnt++;
            }

            System.out.print("\n");
        }
    }
}


